package com.springbootforest.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class testService {

    @Scheduled(cron = "0 0 0/2 * * ?") //设置定时任务，执行方法
    public ResponseWrapper updateWeather(){
        List<Scenic> scenicList = scenicMapper.selectList(null); //从数据库获取景区经纬度
        for (Scenic scenic :scenicList){
            String localtion = scenic.getScenicLongitude()+","+scenic.getScenicLatitude(); //获取目标地方的经纬度
            Map weather = myClient.getWeather(localtion); //传入经纬度，然后查询实时天气
            Map result = (Map) weather.get("result");
            Map realtime = (Map) result.get("realtime");
            String temperature = realtime.get("temperature").toString();//获取温度
            String skycon = realtime.get("skycon").toString(); //获取下雨或者晴天的信息
            String skycons = null;
            if (skycon.equals("CLEAR_DAY")){
                skycons="晴（白天）";
            }else if (skycon.equals("CLEAR_NIGHT")){
                skycons="晴（夜间）";
            }
            else if (skycon.equals("PARTLY_CLOUDY_DAY")){
                skycons="多云（白天）";
            }
            else if (skycon.equals("PARTLY_CLOUDY_NIGHT")){
                skycons="多云（夜间）";
            }
            else if (skycon.equals("CLOUDY")){
                skycons="阴";
            }
            else if (skycon.equals("LIGHT_HAZE")){
                skycons="轻度雾霾";
            }
            else  if (skycon.equals("MODERATE_HAZE")){
                skycons="中度雾霾";
            }
            else if (skycon.equals("HEAVY_HAZE")){
                skycons="重度雾霾";
            }
            else if (skycon.equals("LIGHT_RAIN")){
                skycons="小雨";
            }
            else if (skycon.equals("MODERATE_RAIN")){
                skycons="中雨";
            }
            else  if (skycon.equals("HEAVY_RAIN")){
                skycons="大雨";
            }
            else if (skycon.equals("STORM_RAIN")){
                skycons="暴雨";
            }
            else if (skycon.equals("FOG")){
                skycons="雾";
            }
            else if (skycon.equals("LIGHT_SNOW")){
                skycons="小雪";
            }
            else  if (skycon.equals("MODERATE_SNOW")){
                skycons="中雪";
            }
            else  if (skycon.equals("HEAVY_SNOW")){
                skycons="大雪";
            }
            else  if (skycon.equals("STORM_SNOW")){
                skycons="暴雪";
            }
            else if (skycon.equals("DUST")){
                skycons="浮尘";
            }
            else  if (skycon.equals("SAND")){
                skycons="沙尘";
            }
            else if (skycon.equals("WIND")){
                skycons="大风";
            }
            String weathers = temperature.substring(0,temperature.indexOf(".")) + "°C "+skycons;
            scenic.setWeather(weathers);
            scenicMapper.updateById(scenic); //将天气信息存入数据库
        }
        return null;
    }

}
